package com.syh.jn.scm.infrastructure.repository.impl.supplier.finance;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AggregateRepoMybatisImplTmpl;
import com.syh.jn.scm.domain.supplier.repository.finance.OrderExpensesBillReadRepository;
import com.syh.jn.scm.dto.supplier.finance.orderExpensesBill.OrderExpensesBillItemVo;
import com.syh.jn.scm.dto.supplier.finance.orderExpensesBill.OrderExpensesBillVo;
import com.syh.jn.scm.infrastructure.repository.model.finance.OrderExpensesBillItemModel;
import com.syh.jn.scm.infrastructure.repository.model.finance.OrderExpensesBillModel;
import com.syh.jn.scm.infrastructure.repository.utils.MybatisTemplate;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

@Repository("orderExpensesBillReadRepositoryMbImpl")
public class OrderExpensesBillReadRepositoryMbImpl implements OrderExpensesBillReadRepository {
  private MybatisTemplate mybatisTemplate;
  private AggregateRepoMybatisImplTmpl<OrderExpensesBillVo, OrderExpensesBillModel, Long> orderExpensesBillRepoMybatisImplTmpl =
          new AggregateRepoMybatisImplTmpl.Builder().listTypeClass(OrderExpensesBillVo.class).modelClass(OrderExpensesBillModel.class).build();
  private AggregateRepoMybatisImplTmpl<OrderExpensesBillItemVo, OrderExpensesBillItemModel, Long> orderExpensesBillItemRepoMybatisImplTmpl =
          new AggregateRepoMybatisImplTmpl.Builder().listTypeClass(OrderExpensesBillItemVo.class).modelClass(OrderExpensesBillItemModel.class).build();

  @Override
  public OrderExpensesBillVo getById(Long aLong) {
    OrderExpensesBillVo orderExpensesBillVo =  this.orderExpensesBillRepoMybatisImplTmpl.getById(aLong);
    orderExpensesBillVo.setItems(this.getBillItemsByBillId(aLong));
    return orderExpensesBillVo;
  }

  @Override
  public OrderExpensesBillVo getOne(Wrapper wrapper) {
    return this.orderExpensesBillRepoMybatisImplTmpl.getOne(wrapper);
  }

  @Override
  public boolean contains(Long aLong) {
    return this.getById(aLong) == null ? false :true;
  }

  @Override
  public List<OrderExpensesBillVo> getList(Wrapper wrapper) {
    return this.orderExpensesBillRepoMybatisImplTmpl.getList(wrapper);
  }

  @Override
  public List<OrderExpensesBillVo> findListPage(Wrapper wrapper, Integer startRow, Integer count) {
    return this.orderExpensesBillRepoMybatisImplTmpl.findListPage(wrapper,startRow,count);
  }

  @Override
  public int getCount(Wrapper wrapper) {
    return this.orderExpensesBillRepoMybatisImplTmpl.getCount(wrapper);
  }

  /**
   * 根据订单费用表id获取费用明细
   * @param id
   * @return
   */
  @Override
  public List<OrderExpensesBillItemVo> getBillItemsByBillId(Long id) {
    Wrapper wrapper = Wrapper.build().append(Restrictions.eq("id",id));
    List<OrderExpensesBillItemVo> orderExpensesBillItemVos = this.orderExpensesBillItemRepoMybatisImplTmpl.getList(wrapper);
    return orderExpensesBillItemVos;
  }

  public OrderExpensesBillReadRepositoryMbImpl() {
    // 初始化查询语句 orderExpensesBill
    SQL orderExpensesBillSql = new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(OrderExpensesBillModel.class,
                      "orderExpensesBill", null),
              "account.NAME AS brokerageName"
      );
      FROM("T_FINANCE_ORDER_EXPENSES_BILL orderExpensesBill");
      LEFT_OUTER_JOIN("T_SYSTEM_ACCOUNT account ON orderExpensesBill.BROKERAGE_ID = account.ID");
    }};
    this.orderExpensesBillRepoMybatisImplTmpl.setQuerySql(orderExpensesBillSql);
    //初始化查询条件解析 orderExpensesBillMap
    final HashMap<String, String> orderExpensesBillMap = new HashMap<>();
    orderExpensesBillMap.put("id", "orderExpensesBill.ID");
    orderExpensesBillMap.put("sourceId", "orderExpensesBill.SOURCE_ID");
    orderExpensesBillMap.put("sourceCode", "orderExpensesBill.SOURCE_CODE");
    orderExpensesBillMap.put("orderType", "orderExpensesBill.ORDER_TYPE");
    orderExpensesBillMap.put("state", "orderExpensesBill.STATE");
    orderExpensesBillMap.put("brokerageId", "orderExpensesBill.BROKERAGE_ID");
    orderExpensesBillMap.put("createTimeStamp", "orderExpensesBill.CREATE_TIME_STAMP");
    orderExpensesBillMap.put("lastUpdateTimestamp", "orderExpensesBill.LAST_UPDATE_TIMESTAMP");
    orderExpensesBillMap.put("businessEntityId", "orderExpensesBill.BUSINESS_ENTITY_ID");
    this.orderExpensesBillRepoMybatisImplTmpl.setWrapperParseMethods((propertyName) -> {
      return orderExpensesBillMap.get(propertyName);
    });

    // 初始化查询语句 orderExpensesBillItem
    SQL orderExpensesBillItemSql = new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(OrderExpensesBillItemModel.class,
                      "orderExpensesBillItem", null)
      );
      FROM("T_FINANCE_ORDER_EXPENSES_BILL_ITEM orderExpensesBillItem");
      LEFT_OUTER_JOIN("T_FINANCE_ORDER_EXPENSES_BILL orderExpensesBill ON orderExpensesBill.ID = orderExpensesBillItem.EXPENSES_BILL_ID");
    }};
    this.orderExpensesBillItemRepoMybatisImplTmpl.setQuerySql(orderExpensesBillItemSql);
    //初始化查询条件解析 orderExpensesBillMap
    final HashMap<String, String> orderExpensesBillItemMap = new HashMap<>();
    orderExpensesBillItemMap.put("id", "orderExpensesBill.ID");
    orderExpensesBillItemMap.put("sourceId", "orderExpensesBill.SOURCE_ID");
    orderExpensesBillItemMap.put("sourceCode", "orderExpensesBill.SOURCE_CODE");
    orderExpensesBillItemMap.put("orderType", "orderExpensesBill.ORDER_TYPE");
    orderExpensesBillItemMap.put("state", "orderExpensesBill.STATE");
    this.orderExpensesBillItemRepoMybatisImplTmpl.setWrapperParseMethods((propertyName) -> {
      return orderExpensesBillItemMap.get(propertyName);
    });

  }
}
